package com.wtb.javatool.action;

import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Action;

import com.fy.wetoband.utils.RequestUtil;
import com.wtb.javatool.entity.Major;
import com.wtb.javatool.entity.MajorScore;
import com.wtb.javatool.service.IMajorScoreService;
import com.wtb.javatool.service.IMajorService;
import com.wtb.javatool.util.MajorScoreUtil;
import com.wtb.javatool.vo.MajorDto;
import com.wtb.javatool.vo.MajorScoreDto;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Scope;

import com.fy.wetoband.tool.Tool;
import com.wtb.javatool.common.R;


@Api(value = "javatool", description = "java工具示例")
@Scope(SCOPE_PROTOTYPE) //务必使用多实例模式
@ComponentScan("com.wtb.javatool")
public class JavaTool extends Tool {

    @Autowired
    private IMajorService majorService;

    @Autowired
    private IMajorScoreService majorScoreService;

    @Action
    @ApiOperation(value = "列出所有专业数据", httpMethod = "GET", notes = "列出所有专业数据")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = R.class)
    })
    public R<List<MajorDto>> listAll(HttpServletRequest request, HttpServletResponse response) {
        List<Major> majors = majorService.listAll();

        List<MajorDto> list = majors.stream().map(new Function<Major, MajorDto>() {

            @Override
            public MajorDto apply(Major major) {
                MajorDto dto = new MajorDto();
                BeanUtils.copyProperties(major, dto);
                return dto;
            }
        }).collect(Collectors.toList());
        return R.success(list);
    }

    @Action
    @ApiOperation(value = "添加专业评估", httpMethod = "POST", notes = "添加专业评估")
    @ApiResponses({
            @ApiResponse(code = 200, message = "添加专业评估表成功", response = R.class)
    })
    public R<String> saveMajorScore(HttpServletRequest request, HttpServletResponse response) {

        MajorScore majorScore = MajorScoreUtil.getMajorScore(request);
        majorScoreService.save(majorScore);
        return R.success("添加专业评估表成功");
    }

    @Action
    @ApiOperation(value = "禁用专业", httpMethod = "POST", notes = "禁用专业")
    @ApiResponses({
            @ApiResponse(code = 200, message = "禁用专业成功", response = R.class)
    })
    public R<String> disabledMajor(HttpServletRequest request, HttpServletResponse response) {
        String majorCode = RequestUtil.getStringParameter(request, "majorCode");
        Major major = new Major();
        major.setMajorCode(majorCode);
        majorService.disableMajor(major);
        return R.success("禁用专业成功");
    }

    @Action
    @ApiOperation(value = "解禁专业", httpMethod = "POST", notes = "解禁专业")
    @ApiResponses({
            @ApiResponse(code = 200, message = "解禁专业成功", response = R.class)
    })
    public R<String> unDisabledMajor(HttpServletRequest request, HttpServletResponse response) {
        String majorCode = RequestUtil.getStringParameter(request, "majorCode");
        Major major = new Major();
        major.setMajorCode(majorCode);
        majorService.unDisableMajor(major);
        return R.success("解禁专业成功");
    }

    @Action
    @ApiOperation(value = "暂存专业评估", httpMethod = "POST", notes = "暂存专业评估")
    @ApiResponses({
            @ApiResponse(code = 200, message = "暂存专业评估表成功", response = R.class)
    })
    public R<String> tempMajorScore(HttpServletRequest request, HttpServletResponse response) {
        MajorScore majorScore = MajorScoreUtil.getMajorScore(request);
        majorScoreService.temp(majorScore);
        return R.success("暂存专业评估表成功");
    }

    @Action
    @ApiOperation(value = "通过专业编号查询是否有填评估数据", httpMethod = "POST", notes = "通过专业编号查询是否有填评估数据")
    @ApiResponses({
            @ApiResponse(code = 200, message = "通过专业编号查询是否有填评估数据", response = R.class)
    })
    public R<MajorScoreDto> selectByMajorCode(HttpServletRequest request, HttpServletResponse response) {
        String majorCode = RequestUtil.getStringParameter(request, "majorCode");
        MajorScoreDto majorScoreDto = majorScoreService.selectByMajorCode(majorCode);
        if (majorScoreDto != null) majorScoreDto.setFormList();
        return R.success(majorScoreDto);
    }

    @Action
    @ApiOperation(value = "审核通过", httpMethod = "POST", notes = "审核通过")
    @ApiResponses({
            @ApiResponse(code = 200, message = "审核通过", response = R.class)
    })
    public R<String> approve(HttpServletRequest request, HttpServletResponse response) {
        String majorCode = RequestUtil.getStringParameter(request, "majorCode");
        MajorScore majorScore = new MajorScore();
        majorScore.setMajorCode(majorCode);
        majorScoreService.approve(majorScore);
        return R.success("审核通过");
    }

    @Action
    @ApiOperation(value = "审核不通过", httpMethod = "POST", notes = "审核不通过")
    @ApiResponses({
            @ApiResponse(code = 200, message = "审核不通过", response = R.class)
    })
    public R<String> disApprove(HttpServletRequest request, HttpServletResponse response) {
        String majorCode = RequestUtil.getStringParameter(request, "majorCode");
        MajorScore majorScore = new MajorScore();
        majorScore.setMajorCode(majorCode);
        majorScoreService.disApprove(majorScore);
        return R.success("审核不通过");
    }
}
